 $(function(){
    var header='<p>date</p><h3><span class="time">times</span><b>type</b><i>cost</i></h3>',
        callee='<dl><dt><span>phone</span></dt><dd>duration</dd></dl>',
        url=localCache.getSessionItem('history_detail_url'),
        account_identify=localCache.getSessionItem('account_identify'),
    		noresult=$('#noresult'),
    		content=$('#content'),
        loadPrompt;
        abilityType={
        	  TalksV2:'多方通话',
    	      ScheduleConference:'专业会议',
    	      Dial:'两方通话',
    	      Dial_ronglian:'两方通话',
    	      VoiceNotice3:'语音通知',
    	      RongLianVoice:'语音通知',
    	      TianYiVoice:'语音通知',
    	      AudeoSend:'语音通知',
    	      RongLianAudeo:'语音通知',
    	      TianYiAudeo:'语音通知',
    	      Vcaptcha:'语音验证码',
    	      Ivr_RongL:'IVR外呼',
    	      Ivr_Ims:'IVR外呼'
        },timeSection={
          '3d':{
            interval:3,
            unit:'days',
            id:'#3days'
          },
          '1w':{
            interval:1,
            unit:'weeks',
            id:'#1week'
          },
          '1m':{
            interval:1,
            unit:'months',
            id:'#1month'
          },
          '3m':{
            interval:3,
            unit:'months',
            id:'#3months'
          }
        };

   Array.prototype.search=function(value){
     for (var i = 0; i < this.length; i++) {
        if(this[i]===value||value.indexOf(this[i])>-1)
          return {index:i,element:this[i]};
     };
     return -1;
   }
   
   var showTitle=localCache.getSessionItem('showhdtitle');
   if(!showTitle){
     $('#header').css('display','none');
     $('#context').css('margin-top',0);
   }

   $('#back').on('click',function(){
     history.back();
   });

   $('.history h2 span').on('click',function(){
      var l=$(this),loaded=l.attr('loaded'),id=l.attr('id'),context=$(timeSection[id].id);
      //if(!loaded){//第一次加载
      var queryParameter={startTime:moment().subtract(timeSection[id].interval,timeSection[id].unit).format('YYYY-MM-DD 00:00:00')};
      var isAdmin=localCache.getSessionItem('isAdmin');
      if(isAdmin==='false')
    	  queryParameter.ext_terminalCode=localCache.getSessionItem('userId');
        queryMeetingHistoryDetail(queryParameter,{
          content:context,
          label:l
        });
      //}
      l.addClass('current').siblings().removeClass();
      context.addClass('current').siblings().removeClass();
    });
   $('.history h2 span').first().trigger('click');
   
    function queryMeetingHistoryDetail(params,elems){
       $.ajax({
          type: "get",
          url: url,
          dataType: "jsonp",
          data:$.extend({
        	  cust_account:account_identify,
        	  app_key:localCache.getSessionItem('account_identifer'),
        	  ability_type:''
//        	  ability_type:'TalksV2,ScheduleConference'
            },params),
          jsonp: "callback",
          success: function(data){
              if(loadPrompt){
	              loadPrompt.close();
	              loadPrompt=null;
              }        		 
			if(data.length>0){
				var html=[],time,phones,callees,calleeCost;
				for (var i = 0; i < data.length; i++) {
				  time=moment(data[i].createtime,'YYYY-MM-DD HH:mm:ss');
				  
				  if(data[i].ability_type==='Dial' || data[i].ability_type==='Dial_ronglian' ||
						  data[i].ability_type==='TalksV2' || data[i].ability_type==='ScheduleConference'){
					  phones=[data[i].caller].concat(data[i].phones.split(',')); 
				  }else{
					  phones=data[i].phones.split(','); 
				  }
				  
				  callees=data[i].mem.split(',');
				  calleeCost=data[i].times.split(',');
				  html.push(header.replace(/date|times|type|cost/g,function(value){
					 if(value==='date')
					   return time.format('YYYY年MM月DD日');
					 else if(value==='times')
					   return time.format('HH:mm');
					 else if(value==='type')
					   return abilityType[data[i].ability_type]+'（'+phones.length+'人)';
					 else if(value==='cost')
					   return data[i].cost+'分钟';
					 else
					   return value;
				   }));
				//meet_info_callback 
				  for (var m = 0; m < callees.length&&callees[m]; m++) {
					  var target=phones.search(callees[m]);
					  if(target=="-1"){
						  target ={
							index:-1,
							element:callees[m]	  
						  };
					  }
					  var index=target.index;
					  html.push(callee.replace(/phone|duration/g,function(value){
						 if(value==='phone')
						  return target.element;
						 else if(value==='duration'){
							  if(data[i].ability_type==='Dial' || data[i].ability_type==='Dial_ronglian')//两方通话不展示通话时长
								  return '';
							  else{
								  if(calleeCost[m] ===undefined || calleeCost[m] ==='')
									  return "0分钟";
								  else
									  return Math.ceil(calleeCost[m]/60)+'分钟';
							  }
						 }
					  }));
					  if(index!==-1)
						phones.splice(index,1);
				  };
				 
				  for (var n = 0; n < phones.length&&phones[n]; n++) {
					   html.push(callee.replace(/phone|duration/g,function(value){
						 if(value==='phone')
						  return phones[n];
						 else if(value==='duration'){
							  if(data[i].ability_type==='Dial' || data[i].ability_type==='Dial_ronglian')//两方通话不展示通话时长
								  return '';
							  else{
								  if(calleeCost[m] ===undefined || calleeCost[m] ==='')
									  return "0分钟";
								  else
									  return Math.ceil(calleeCost[m]/60)+'分钟';
							  }
								  
						 }
					  }));
				   };
				 };
				 elems.content.html(html.join(''));
				 //elems.label.attr('loaded',true);
			}else{
				 noresult.css('display','block');
				 content.css('display','none');
			}
          },
          complete:function(){
             if(loadPrompt){
               loadPrompt.close();
               loadPrompt=null;
             }
          },
          beforeSend:function(){
			 elems.content.html('');
  			 content.css('display','block');
  			 noresult.css('display','none');
             loadPrompt=new LoadPrompt('正在查询，请稍后',$(document.body),function(){
//                var height=$(document).height(),headerHeight=$('#header').outerHeight(true)+$('#title').outerHeight(true);
//                this.height(height-headerHeight);
//                this.css('top',headerHeight);
            	var obj=$(document);
            	this.height(obj.height());
                this.width(obj.width());
             })
          }
      });
    }
  })
	
